package cn.chinaiptv.serverlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.chinaiptv.bean.User;
import cn.chinaiptv.user.UserReaderWriter;
import cn.chinaiptv.utils.ParamsFromConfFile;

/**
 * Servlet implementation class changePasswordServlet
 */
public class ChangePasswordServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChangePasswordServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String currentPassword = request.getParameter("currentPassword");
		String newPassword = request.getParameter("newPassword");
		System.out.println(newPassword);
		UserReaderWriter urw = new UserReaderWriter();
		String realPassword = urw.readUserPassword("user1");
		if(realPassword.equals(currentPassword)){
		boolean result = urw.writeProperties("user1", newPassword);
			if(result){
				//修改成功
				//
				out.write("修改成功");
				return;
			}else{
				//修改失败
				out.write("操作失败.2秒后转向修改密码页面请重新输入");
				response.setHeader("Refresh", "2;URL="+request.getContextPath()+"/editPassword.jsp");
				return;
			}
		}
		else{
			//输入的当前密码不正确
			out.write("输入的当前密码不正确.2秒后转向修改密码页面请重新输入");
			response.setHeader("Refresh", "2;URL="+request.getContextPath()+"/editPassword.jsp");
			return;
			
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
